unit Class_WORK;

interface
uses
  Classes,SysUtils,Uni,UniEngine;

type
  TWorkObjtType=(wotWork,wotDoct);
  
  TWORK=class(TUniEngine)
  private
    FUNITLINK: string;
    FWORKIDEX: Integer;
    FWORKNAME: string;
    FWORKCODE: string;
    FWORKMOBL: string;
    FWORKCALL: string;
    FWORKMEMO: string;
    FWORKAREA: string;
  public
    FListDoct: TStringList;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property UNITLINK: string read FUNITLINK write FUNITLINK;
    property WORKIDEX: Integer read FWORKIDEX write FWORKIDEX;
    property WORKNAME: string read FWORKNAME write FWORKNAME;
    property WORKCODE: string read FWORKCODE write FWORKCODE;
    property WORKMOBL: string read FWORKMOBL write FWORKMOBL;
    property WORKCALL: string read FWORKCALL write FWORKCALL;
    property WORKMEMO: string read FWORKMEMO write FWORKMEMO;
    property WORKAREA: string read FWORKAREA write FWORKAREA;     
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(AWORK:TWORK):TWORK;overload;        
    class procedure CopyIt(AWORK:TWORK;var Result:TWORK);overload;       
  end;

implementation

uses
  Class_KzUtils;

{ TWORK }
procedure TWORK.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('WORK_IDEX').Value := WORKIDEX;
        ParamByName('WORK_NAME').Value := WORKNAME;
        ParamByName('WORK_CODE').Value := WORKCODE;
        ParamByName('WORK_MOBL').Value := WORKMOBL;
        ParamByName('WORK_CALL').Value := WORKCALL;
        ParamByName('WORK_MEMO').Value := WORKMEMO;
        ParamByName('WORK_AREA').Value := WORKAREA;
      end;
      otEdit:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('WORK_IDEX').Value := WORKIDEX;
        ParamByName('WORK_NAME').Value := WORKNAME;
        ParamByName('WORK_CODE').Value := WORKCODE;
        ParamByName('WORK_MOBL').Value := WORKMOBL;
        ParamByName('WORK_CALL').Value := WORKCALL;
        ParamByName('WORK_MEMO').Value := WORKMEMO;
        ParamByName('WORK_AREA').Value := WORKAREA;
      end;  
      otDelt:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('WORK_IDEX').Value := WORKIDEX;
 
      end;  
    end;
  end;
end;

function TWORK.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_WORK',['UNIT_LINK',UNITLINK,'WORK_IDEX',WORKIDEX],AUniConnection);
end;

function TWORK.GetNextIdex: Integer;
begin

end;

function TWORK.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin
  Result:=CheckField('WORK_IDEX',TablNam,['UNIT_LINK',FUNITLINK],AUniConnection);
end;

function TWORK.GetStrDelete: string;
begin
  Result:='DELETE FROM $TBL_WORK WHERE   UNIT_LINK=:UNIT_LINK AND WORK_IDEX=:WORK_IDEX';
  Result:=StringReplace(Result,'$TBL_WORK',TablNam,[rfReplaceAll]);
end;

function TWORK.GetStrInsert: string;
begin
  Result:='INSERT INTO $TBL_WORK'
         +'    ( UNIT_LINK, WORK_IDEX, WORK_NAME, WORK_CODE, WORK_MOBL'
         +'    , WORK_CALL, WORK_MEMO, WORK_AREA)'
         +'    VALUES'
         +'    (:UNIT_LINK,:WORK_IDEX,:WORK_NAME,:WORK_CODE,:WORK_MOBL'
         +'    ,:WORK_CALL,:WORK_MEMO,:WORK_AREA)';

  Result:=StringReplace(Result,'$TBL_WORK',TablNam,[rfReplaceAll]);
end;

function TWORK.GetStrsIndex: string;
begin
  Result:=Format('%S-%D',[UNITLINK,WORKIDEX]);
end;

function TWORK.GetStrUpdate: string;
begin
  Result:='UPDATE  $TBL_WORK SET'
         +'    WORK_NAME=:WORK_NAME,'
         +'    WORK_CODE=:WORK_CODE,'
         +'    WORK_MOBL=:WORK_MOBL,'
         +'    WORK_CALL=:WORK_CALL,'
         +'    WORK_MEMO=:WORK_MEMO,'
         +'    WORK_AREA=:WORK_AREA'
         +'    WHERE UNIT_LINK=:UNIT_LINK'
         +'    AND   WORK_IDEX=:WORK_IDEX'; 
  Result:=StringReplace(Result,'$TBL_WORK',TablNam,[rfReplaceAll]);
end;

constructor TWORK.Create;
begin
  TablNam  :='TBL_WORK';
  UNITLINK :='-1';
  FListDoct:=nil;
end;

destructor TWORK.Destroy;
begin
  if FListDoct<>nil then TKzUtils.TryFreeAndNil(FListDoct);
  inherited;
end;

class function TWORK.ReadDS(AUniQuery: TUniQuery): TUniEngine;
begin
  Result:=TWORK.Create;
  with TWORK(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    WORKIDEX:=AUniQuery.FieldByName('WORK_IDEX').AsInteger;
    WORKNAME:=AUniQuery.FieldByName('WORK_NAME').AsString;
    WORKCODE:=AUniQuery.FieldByName('WORK_CODE').AsString;
    WORKMOBL:=AUniQuery.FieldByName('WORK_MOBL').AsString;
    WORKCALL:=AUniQuery.FieldByName('WORK_CALL').AsString;
    WORKMEMO:=AUniQuery.FieldByName('WORK_MEMO').AsString;
    WORKAREA:=AUniQuery.FieldByName('WORK_AREA').AsString;
  end;  
end;

class procedure TWORK.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
begin
  if Result=nil then Exit;
  with TWORK(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    WORKIDEX:=AUniQuery.FieldByName('WORK_IDEX').AsInteger;
    WORKNAME:=AUniQuery.FieldByName('WORK_NAME').AsString;
    WORKCODE:=AUniQuery.FieldByName('WORK_CODE').AsString;
    WORKMOBL:=AUniQuery.FieldByName('WORK_MOBL').AsString;
    WORKCALL:=AUniQuery.FieldByName('WORK_CALL').AsString;
    WORKMEMO:=AUniQuery.FieldByName('WORK_MEMO').AsString;
    WORKAREA:=AUniQuery.FieldByName('WORK_AREA').AsString;
  end;  
end;

class function  TWORK.CopyIt(AWORK: TWORK): TWORK;
begin
  Result:=TWORK.Create;
  TWORK.CopyIt(AWORK,Result)
end;

class procedure TWORK.CopyIt(AWORK:TWORK;var Result:TWORK);
begin
  if Result=nil then Exit;
  Result.UNITLINK:=AWORK.UNITLINK;
  Result.WORKIDEX:=AWORK.WORKIDEX;
  Result.WORKNAME:=AWORK.WORKNAME;
  Result.WORKCODE:=AWORK.WORKCODE;
  Result.WORKMOBL:=AWORK.WORKMOBL;
  Result.WORKCALL:=AWORK.WORKCALL;
  Result.WORKMEMO:=AWORK.WORKMEMO;
  Result.WORKAREA:=AWORK.WORKAREA;
end;

end.
